<?php

namespace app\common\controller;

use Exception;
use think\Controller;

class AilPay extends Controller
{
    public function toPay($orderInfo,$config,$type)
    {
        require_once VENDOR_PATH . 'Alipay/AopSdk.php';
        $aop = new \AopClient();
        $aop->signType = 'RSA2';
        if(!empty($config)){
            $aop->appId = $config['appid'];
            $aop->rsaPrivateKey = $config['rsaprivatekey'];
        }
        switch ($type) {
            case 'wap':
                $productcode = 'QUICK_WAP_PAY';
                $request = new \AlipayTradeWapPayRequest();
                $request->setReturnUrl($orderInfo['return_url']);
                $request->setNotifyUrl($orderInfo['notify_url']);
                $request->setBizContent('{"product_code":"' . $productcode . '","body":"' . $orderInfo['body'] . '","subject":"' . $orderInfo['props_name'] . '","total_amount":"' . $orderInfo['pay_amount'] . '","out_trade_no":"' . $orderInfo['pay_order_number'] . '"}');
                return $aop->pageExecute($request, 'GET');
                break;
            default:
                $productcode = 'FAST_INSTANT_TRADE_PAY';
                $request = new \AlipayTradePagePayRequest();
                break;
        }

    }

    public function toSerPay($orderInfo,$config,$type)
    {
        require_once VENDOR_PATH . 'Alipay/AopSdk.php';
        $aop = new \AopClient();
        $aop->signType = 'RSA2';
        if(!empty($config)){
            $aop->appId = $config['appid'];
            $aop->rsaPrivateKey = $config['rsaprivatekey'];
        }
        switch ($type) {
            case 'wap':
                $productcode = 'QUICK_WAP_PAY';
                $request = new \AlipayTradeWapPayRequest();
                $request->setReturnUrl($orderInfo['return_url']);
                $request->setNotifyUrl($orderInfo['notify_url']);
                $request->setBizContent('{"product_code":"' . $productcode . '","subject":"' . $orderInfo['ser_body'] . '","total_amount":"' . $orderInfo['price'] . '","out_trade_no":"' . $orderInfo['order_num'] . '"}');
                return $aop->pageExecute($request, 'GET');
                break;
            default:
                $productcode = 'FAST_INSTANT_TRADE_PAY';
                $request = new \AlipayTradePagePayRequest();
                break;
        }

    }

    public function toYunPay($orderInfo,$config,$type)
    {
        require_once VENDOR_PATH . 'Alipay/AopSdk.php';
        $aop = new \AopClient();
        $aop->signType = 'RSA2';
        if(!empty($config)){
            $aop->appId = $config['appid'];
            $aop->rsaPrivateKey = $config['rsaprivatekey'];
        }
        switch ($type) {
            case 'wap':
                $productcode = 'QUICK_WAP_PAY';
                $request = new \AlipayTradeWapPayRequest();
                $request->setReturnUrl($orderInfo['return_url']);
                $request->setNotifyUrl($orderInfo['notify_url']);
                $request->setBizContent('{"product_code":"' . $productcode . '","subject":"' . $orderInfo['goods_name'] . '","total_amount":"' . $orderInfo['price'] . '","out_trade_no":"' . $orderInfo['order_num'] . '","quit_url":"' . $orderInfo['quit_url'] . '"}');
                return $aop->pageExecute($request, 'GET');
                break;
            default:
                $productcode = 'FAST_INSTANT_TRADE_PAY';
                $request = new \AlipayTradePagePayRequest();
                break;
        }

    }


    public function toRechargePay($orderInfo,$config,$type)
    {
        require_once VENDOR_PATH . 'Alipay/AopSdk.php';
        $aop = new \AopClient();
        $aop->signType = 'RSA2';
        if(!empty($config)){
            $aop->appId = $config['appid'];
            $aop->rsaPrivateKey = $config['rsaprivatekey'];
        }
        switch ($type) {
            case 'wap':
                $productcode = 'QUICK_WAP_PAY';
                $request = new \AlipayTradeWapPayRequest();
                $request->setReturnUrl($orderInfo['return_url']);
                $request->setNotifyUrl($orderInfo['notify_url']);
                $request->setBizContent('{"product_code":"' . $productcode . '","subject":"' . $orderInfo['title'] . '","total_amount":"' . $orderInfo['discount_price'] . '","out_trade_no":"' . $orderInfo['order_num'] . '","quit_url":"' . $orderInfo['quit_url'] . '"}');
                return $aop->pageExecute($request, 'GET');
                break;
            default:
                $productcode = 'FAST_INSTANT_TRADE_PAY';
                $request = new \AlipayTradePagePayRequest();
                break;
        }

    }


    /**
     * Desc : VIP开通支付
     * User : Steven
     * Date : 2024-10-09 09:17
     */
    public function toVipPay($orderInfo,$config,$type)
    {
        require_once VENDOR_PATH . 'Alipay/AopSdk.php';
        $aop = new \AopClient();
        $aop->signType = 'RSA2';
        if(!empty($config)){
            $aop->appId = $config['appid'];
            $aop->rsaPrivateKey = $config['rsaprivatekey'];
        }
        switch ($type) {
            case 'wap':
                $productcode = 'QUICK_WAP_PAY';
                $request = new \AlipayTradeWapPayRequest();
                $request->setReturnUrl($orderInfo['return_url']);
                $request->setNotifyUrl($orderInfo['notify_url']);
                $request->setBizContent('{"product_code":"' . $productcode . '","subject":"' . $orderInfo['title'] . '","total_amount":"' . $orderInfo['price'] . '","out_trade_no":"' . $orderInfo['order_num'] . '","quit_url":"' . $orderInfo['quit_url'] . '"}');
                return $aop->pageExecute($request, 'GET');
                break;
            default:
                $productcode = 'FAST_INSTANT_TRADE_PAY';
                $request = new \AlipayTradePagePayRequest();
                break;
        }

    }

    public function OrderQuery($ali,$pay_order_number){
        require_once \Env::get('vendor_path') . 'alipay-sdk-PHP-demo/config.php';
        require_once \Env::get('vendor_path') . 'alipay-sdk-PHP-demo/pagepay/service/AlipayTradeService.php';
        require_once \Env::get('vendor_path') . 'alipay-sdk-PHP-demo/pagepay/service/AlipayTradeService.php';
        require_once \Env::get('vendor_path') . 'alipay-sdk-PHP-demo/pagepay/buildermodel/AlipayTradePagePayContentBuilder.php';
        require_once \Env::get('vendor_path') . 'alipay-sdk-PHP-demo/pagepay/buildermodel/AlipayTradeQueryContentBuilder.php';
        $out_trade_no = trim($pay_order_number);
        $RequestBuilder = new \AlipayTradeQueryContentBuilder();
        new \AlipayTradeQueryRequest();
        $RequestBuilder->setOutTradeNo($out_trade_no);
        $config = array (
            'notify_url' => "",
            //同步跳转
            'return_url' => "",
            //编码格式
            'charset' => "UTF-8",
            //签名方式
            'sign_type'=>"RSA2",
            //支付宝网关
            'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
        );
        if($ali){
            $config['app_id'] = $ali['appid'];
            $config['merchant_private_key'] = $ali['rsaprivatekey'];
            $config['alipay_public_key'] = $ali['rsapublickey'];
        }
        $Response = new \AlipayTradeService($config);
        $result=$Response->Query($RequestBuilder);
        return json_decode(json_encode($result),TRUE);
    }
}
